<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <!-- v-on:click="serch"-->
          <el-button type="primary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="contract" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="30">
      </el-table-column>
      <el-table-column prop="ctPartyA" label="甲方" width="80" sortable>
      </el-table-column>
      <el-table-column prop="ctPartyB" label="乙方" width="80" sortable>
      </el-table-column>
      <el-table-column prop="idCardA" label="甲方身份证信息" width="180" sortable>
      </el-table-column>
      <el-table-column prop="idCardB" label="乙方身份证信息" width="180" sortable>
      </el-table-column>
      <!-- :formatter="formatState" -->
      <el-table-column prop="ctAddress" label="租赁地址" width="120" sortable>
      </el-table-column>
      <el-table-column prop="ctLeaseTime" label="租赁时间" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="ctRent" label="租金" min-width="80" sortable>
      </el-table-column>
      <el-table-column prop="detail.name" label="支付方式" min-width="80" sortable>
      </el-table-column>
      <el-table-column prop="housing.hquartersName" label="房屋信息表ID" min-width="80" sortable>
      </el-table-column>
      <el-table-column prop="ctExpirationTime" label="合同到期时间" min-width="150" sortable>
      </el-table-column>
      <el-table-column prop="detail1.name" label="合同状态" min-width="80" sortable>
        <template scope="scope">
          <span style="color: #13ce66" v-if="scope.row.ctStatus ==25">生效</span>
          <span style="color: #e64242" v-else-if="scope.row.ctStatus ==26">未生效</span>
          <span style="color: #c0ccda" v-else>作废</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="290">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="甲方" prop="ctPartyA">
          <el-input v-model="editForm.ctPartyA" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="甲方身份证" prop="idCardA">
          <el-input v-model="editForm.idCardA" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="乙方" prop="ctPartyB">
          <el-input v-model="editForm.ctPartyB" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="乙方身份证" prop="idCardB">
          <el-input v-model="editForm.idCardB" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="房屋小区">
          <el-select v-model="editForm.houseId" placeholder="请选择">
            <el-option
                v-for="item in houses"
                :label="item.hquartersName"
                :value="item.hid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租赁地址" prop="ctAddress">
          <el-input v-model="editForm.ctAddress" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="租赁时间" prop="ctLeaseTime">
          <el-date-picker clearable
                          v-model="editForm.ctLeaseTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择租赁时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="租金" prop="ctRent">
          <el-input v-model="editForm.ctRent" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="支付方式" prop="ctPaymentType">
          <el-select v-model="editForm.ctPaymentType" placeholder="请选择">
            <el-option
                v-for="item in ctPaymentTypes"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同到期时间" prop="ctExpirationTime">
          <el-date-picker clearable
                          v-model="editForm.ctExpirationTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同状态" prop="ctStatus">
          <el-select v-model="editForm.ctStatus" placeholder="请选择">
            <el-option
                v-for="item in ctStatuss"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      table: false,
      options: [],
      ctPaymentTypes: [],
      ctStatuss: [],
      houses: [],
      // 四大小金刚
      totals: 0,
      pageSize: 5,
      currentPage: 1,
      filters: {
        keyword: '',
        name: ''
      },
      // 获取后台数据
      contract: [],
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          {required: true, message: '请输入部门名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ],
      },
      //编辑界面数据
      editForm: {
        ctId: null,
        ctPartyA: null,
        ctPartyB: null,
        idCardA: null,
        idCardB: null,
        ctAddress: null,
        ctLeaseTime: null,
        ctRent: null,
        ctPaymentType: null,
        houseId: null,
        ctExpirationTime: null,
        ctStatus: null
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //新增界面数据
      addForm: {
        ctId: null,
        ctPartyA: null,
        ctPartyB: null,
        idCardA: null,
        idCardB: null,
        ctAddress: null,
        ctLeaseTime: null,
        ctRent: null,
        ctPaymentType: null,
        houseId: null,
        ctExpirationTime: null,
        ctStatus: null
      }

    }
  },
  methods: {


    // 分页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getHousing();
    },
    //获取用户列表
    getHousing() {
      let para = {
        currentPage: this.currentPage,
        keyword: this.filters.keyword,
        pageSize: this.pageSize,
      };
      // 忙等
      this.listLoading = true;
      // 发送请求：获取后台数据
      this.$http.post("/contract/list", para)
          .then(result => {
            this.contract = result.data.rows;
            this.totals = result.data.totals;
            this.listLoading = false;
          })
          .catch(result => {
            this.$message({
              message: '网络不通',
              type: 'error'
            });
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // 发送请求
        this.$http.delete("/contract/" + row.ctId)
            .then(result => {
              this.listLoading = false;
              if (result.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                })
              }
              this.currentPage = 1,
                  this.getHousing();
            })
            .catch(result => {
              this.$message({
                    message: '网络异常',
                    type: 'error'
                  },
              );
            })
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
      console.log(this.editForm)
    },
    //显示新增界面
    handleAdd: function () {
      this.editFormVisible = true;
      this.addForm = {
        ctId: null,
        ctPartyA: null,
        ctPartyB: null,
        idCardA: null,
        idCardB: null,
        ctAddress: null,
        ctLeaseTime: null,
        ctRent: null,
        ctPaymentType: null,
        houseId: null,
        ctExpirationTime: null,
        ctStatus: null
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            this.editFormVisible = false
            let para = Object.assign({}, this.editForm);
            // 发送请求
            this.$http.put("/contract", para)
                .then(result => {
                  this.listLoading = false;
                  if (result.data.success) {
                    this.$message({
                      message: '操作成功',
                      type: 'success'
                    });
                    this.editLoading = false;
                  } else {
                    this.$message({
                      message: '操作失败',
                      type: 'error'
                    })
                  }
                  this.currentPage = 1,
                      this.getHousing();
                  this.editFormVisible = false;
                })
                .catch(result => {
                  this.$message({
                        message: '网络异常',
                        type: 'error'
                      },
                  );
                })

          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/contract", ids)
            .then(result => {
              this.listLoading = false;
              if (result.data.success) {
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '删除失败',
                  type: 'error'
                })
              }
              this.currentPage = 1,
                  this.getHousing();
            })
            .catch(result => {
              this.$message({
                    message: '网络异常',
                    type: 'error'
                  },
              );
            })
      }).catch(() => {

      });
    },
    // 获取数据字典
    getCtPaymentTypes() {
      this.$http.get("/detail/type/" + 20)
          .then(result => {
            this.ctPaymentTypes = result.data;
          })
    },
    // 获取数据字典
    getCtStatuss() {
      this.$http.get("/detail/type/" + 21)
          .then(result => {
            this.ctStatuss = result.data;
          })
    },
    // 获取房屋
    getHouses() {
      this.$http.get("/housing")
          .then(result => {
            this.houses = result.data;
          })
    }

  },
  mounted() {
    this.getHouses();
    this.getCtPaymentTypes();
    this.getCtStatuss();
    this.getHousing();
  }
}

</script>

<style scoped>

</style>